class KeyBoardState {
  constructor() {
    this.maxHeight = 0
    this.height = 0
    this.events = {}
    this.isFirstShow = false
  }

  init() {
    // 监听键盘高度变化（弹出/收起都会触发）
    this.keyboardListener = wx.onKeyboardHeightChange(e => {
      console.log('note.js -> onKeyboardHeightChange -> e: ', e)

      this.height = e.height

      if (this.maxHeight < e.height) {
        this.maxHeight = e.height
      }
      else {
        this.isFirstShow = true
        this.emitt('shown', this.maxHeight)
      }

      this.emitt('state', e.height)
    });
  }

  getMaxHeight() {
    return this.maxHeight
  }

  isShow() {
    return this.height > 0
  }

  isHide() {
    return this.height === 0
  }

  isFirstShowFinished() {
    return this.isFirstShow
  }

  on(type, handle) {
    if (!this.events[type]) {
      this.events[type] = []
    }
    this.events[type].push(handle)
  }

  emitt(type, data) {
    if (this.events[type]) {
      this.events[type].forEach(f => f(data))
    }
  }
}

const keyBoardState = new KeyBoardState()

module.exports = keyBoardState